export default {

	// nicknamea 用户名  不传默认键名 nicknamea
	// imagea 头像 不传默认键名 imagea
	// codea code码 不传默认键名 imagea
	getUserInfo(nicknamea, imagea, codea) { //小程序登录获取=>头像 昵称 code
		let nickname = nicknamea ? nicknamea : 'nickname'
		let image = imagea ? imagea : 'image'
		let code = codea ? codea : 'code'
		return new Promise((resolve, reject) => {
			uni.getUserProfile({
				lang: 'zh_CN',
				desc: '登录',
				success: res => {
					uni.login({
						provider: 'weixin',
						success: loginRes => {
							let obj = {
								[nickname]: res.userInfo.nickName,
								[image]: res.userInfo.avatarUrl,
								[code]: loginRes.code,
							}
							resolve(obj)
						}
					});
				},
				fail: err => {
					console.log(err);
					reject(err)
				}
			});
		})
	},

	//小程序获取位置信息
	getAddressWx() { //小程序获取位置
		return new Promise((resolve, reject) => {
			// reject(err)
			uni.getSetting({
				success: res => {
					// console.log('用户权限列表:', res.authSetting)
					if (res.authSetting['scope.userFuzzyLocation']) {
						// console.log('已授权userLocation')
						this.getAddressWxCicy().then(resa => {
							// console.log(resa,888);
							resolve(resa)
						}).catch(err => {
							reject(err)
						})
					} else {
						// console.log('用户未授权userLocation')
						//2.用户第一次进来发起授权
						uni.authorize({
							scope: 'scope.userFuzzyLocation',
							success: (res1) => {
								// console.log("允许授权", res1);
								// 3.如果用户点击了允许，调用选择位置信息函数选择位置
								this.getAddressWxCicy().then(resa => {
									resolve(resa)
								}).catch(err => {
									reject(err)
								})
							},
							fail: (err1) => {
								// console.log("拒绝授权", err1);
								// 3.如果用户点击了拒绝或者用户之前点击过拒绝授权，打开用户设置模态框
								uni.showModal({
									title: '',
									content: '获取定位失败，是否授权打开定位',
									success: (res) => {
										if (res.confirm) {
											this.openSetting().then(
												resa => {
													resolve(resa)
												}).catch(err => {
												reject(err)
											})
										} else if (res.cancel) {
											uni.$u.toast('拒绝定位，可能导致功能异常')
										}
									}
								})
							},
						})
					}
				}
			})
		})
	},

	openSetting() { //引导用户打开位置权限
		return new Promise((resolve, reject) => {
			uni.openSetting({
				success: (res) => {
					// console.log("打开设置成功", res);
					if (res.authSetting['scope.userFuzzyLocation']) {
						// console.log('成功授权userLocation')
						// 5.用户在设置中点击了允许，调用选择位置信息函数
						this.getAddressWxCicy().then(resa => {
							resolve(resa)
						}).catch(err => {
							reject(err)
						})
					} else {
						// console.log('用户未授权userLocation')
						// 5.用户在设置中点击了不允许，展示拒绝授权信息
						uni.$u.toast('拒绝定位，可能导致功能异常')
					}
				},
				fail: (err) => {
					console.log("打开设置失败", err)
				}
			})
		})
	},

	getAddressWxCicy() { //已授权获取位置信息
		return new Promise((resolve, reject) => {
			uni.getFuzzyLocation({
				type: 'wgs84',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},

	getProvinces(longitude, latitude) { //根据经纬度反编码获取地址信息
		return new Promise((resolve, reject) => {
			uni.request({
				url: `https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=${longitude},${latitude}&key=2bc840e45eed909bc022753d4cf324b3`,
				method: 'GET',
				timeout: 10000,
				success: (resb) => {
					console.log(resb, 888);
					if (resb.data.status == 1) {
						resolve(resb.data.regeocode)
					} else {
						uni.$u.toast('请输入正确的经纬度')
					}
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},



	//保存图片
	// url 图片的路径
	// isPath 是否需要通过uni.getImageInfo获取本地图片

	dowLoadImg(url, isPath = true) { //判断保存图片权限
		return new Promise((resolve, reject) => {
			uni.authorize({
				scope: 'scope.writePhotosAlbum',
				success: (res) => {
					this.saveImgWx(url, isPath)
				},
				fail: err => {
					// console.log('取消保存')
					// uni.$u.toast('保存失败')
					uni.getSetting({
						success: (res) => {
							// console.log(res.authSetting)
							if (!res.authSetting['scope.writePhotosAlbum']) {
								this.opensit(url, isPath)
							}
						}
					})
				},
			})
		})

	},
	opensit(url, isPath) {
		uni.showModal({
			content: '是否授权小程序保存图片到相册？',
			confirmText: '授权',
			success: (res) => {
				if (res.confirm) {
					uni.openSetting({
						success: (res) => {
							// console.log(res.authSetting)
							this.saveImgWx(url, isPath)
						},
					})
				} else if (res.cancel) {
					// uni.$u.toast('授权失败')
				}
			},
		})
	},
	saveImgWx(url, isPath) { //已授权保存相册 小程序
		if (isPath) {
			uni.getImageInfo({
				src: url,
				success: (res) => {
					uni.saveImageToPhotosAlbum({
						filePath: res.path,
						success: () => {
							uni.$u.toast('保存成功')
						},
						fail: () => {
							uni.$u.toast('保存失败')
						}
					});
				},
				fail: (err) => {
					// console.log(err);
					uni.$u.toast('网络错误请重试')
				}
			});
		} else {
			uni.saveImageToPhotosAlbum({
				filePath: url,
				success: () => {
					uni.$u.toast('保存成功')
				},
				fail: () => {
					uni.$u.toast('保存失败')
				}
			});
		}
	},

	wxPay(e) { //微信小程序支付
		return new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: "wxpay",
				timeStamp: e.timeStamp,
				nonceStr: e.nonceStr,
				package: e.package,
				signType: e.signType,
				paySign: e.paySign,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				}
			});
		})
	},

}
